package com.gyg.executorDemo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 实现多线程下载图片
 */
public class TestThread02 extends Thread {
    /**
     * 下载路径
     */
    private String url;
    /**
     * 文件名
     */
    private String name;

    TestThread02(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downLoader(url,name);
        System.out.println("下载文件名：" + name);
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("https://profile.csdnimg.cn/5/5/A/1_weixin_44985880", "1.png");
        TestThread02 t2 = new TestThread02("https://profile.csdnimg.cn/5/5/A/1_weixin_44985880", "2.png");
        TestThread02 t3 = new TestThread02("https://profile.csdnimg.cn/5/5/A/1_weixin_44985880", "3.png");

        t1.start();
        t2.start();
        t3.start();
    }
}

/**
 * 下载器
 */
class WebDownLoader {
    //下载方法
    public void downLoader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
